package com.example.demo.util;

import cn.hutool.core.util.ObjectUtil;
import com.example.demo.bean.sysmenu.SysMenuDO;
import com.example.demo.dto.sysmenu.TreeRoleMenuItem;
import com.example.demo.dto.sysmenu.TreeRoleMenuRep;
import com.example.demo.dto.sysmenu.TreeSysMenuRep;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysUtil {
    public static List<TreeSysMenuRep> GENERATE_ROUTER(List<SysMenuDO> menus) {
        Map<Long, TreeSysMenuRep> treeMap = new HashMap<>();
        ArrayList<TreeSysMenuRep> rootTree = new ArrayList<>();
        for (SysMenuDO menu : menus) {
            TreeSysMenuRep treeMenu = new TreeSysMenuRep()
                    .setMenuId(menu.getMenuId())
                    .setPath(menu.getPath())
                    .setComponent(ObjectUtil.isNotEmpty(menu.getComponent()) ? menu.getComponent() : "")
                    .setName(menu.getRouteName())
                    .setIcon(menu.getIcon())
                    .setVisible(menu.getVisible())
                    .setTitle(menu.getMenuName())
                    .setParentId(menu.getParentId());
            treeMap.put(menu.getMenuId(), treeMenu);
        }

        for (SysMenuDO menu : menus) {
            Long parentId = menu.getParentId();
            TreeSysMenuRep currentTreeMenu = treeMap.get(menu.getMenuId());
            if (parentId == 0) {
                rootTree.add(currentTreeMenu);
            } else {
                TreeSysMenuRep parentTreeMenu = treeMap.get(parentId);
                if (parentTreeMenu != null) {
                    if (parentTreeMenu.getChildren() == null) {
                        parentTreeMenu.setChildren(new ArrayList<>());
                    }
                    parentTreeMenu.getChildren().add(currentTreeMenu);
                }
            }
        }
        return rootTree;
    }

    public static List<TreeRoleMenuRep> GENERATE_ROLE_MENU_TREE(List<SysMenuDO> menus) {
        Map<Long, TreeRoleMenuRep> treeMap = new HashMap<>();
        ArrayList<TreeRoleMenuRep> rootTree = new ArrayList<>();
        for (SysMenuDO menu : menus) {
            if (!"F".equals(menu.getMenuType())) {
                TreeRoleMenuRep treeMenu = new TreeRoleMenuRep()
                        .setMenuId(menu.getMenuId())
                        .setParentId(menu.getParentId())
                        .setMenuName(menu.getMenuName());
                treeMap.put(menu.getMenuId(), treeMenu);
            }
        }
        for (SysMenuDO menu : menus) {
            if ("F".equals(menu.getMenuType())) {
                continue;
            }

            Long parentId = menu.getParentId();
            TreeRoleMenuRep currentTreeMenu = treeMap.get(menu.getMenuId());

            if (parentId == 0) {
                rootTree.add(currentTreeMenu);
            } else {
                TreeRoleMenuRep parentTreeMenu = treeMap.get(parentId);
                if (parentTreeMenu != null) {
                    if (parentTreeMenu.getChildren() == null) {
                        parentTreeMenu.setChildren(new ArrayList<>());
                    }
                    parentTreeMenu.getChildren().add(currentTreeMenu);
                }
            }
        }
        for (SysMenuDO menu : menus) {
            if ("F".equals(menu.getMenuType())) {
                TreeRoleMenuRep parentTreeMenu = treeMap.get(menu.getParentId());
                if (parentTreeMenu != null) {
                    if (parentTreeMenu.getPerms() == null) {
                        parentTreeMenu.setPerms(new ArrayList<>());
                    }
                    TreeRoleMenuItem permItem = new TreeRoleMenuItem()
                            .setMenuId(menu.getMenuId())
                            .setParentId(menu.getParentId())
                            .setMenuName(menu.getMenuName());
                    parentTreeMenu.getPerms().add(permItem);
                }
            }
        }

        return rootTree;
    }
}
